using System.Linq;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Linq;
using System.Diagnostics;
using System.Threading;

namespace WebRole1
{
	public class WebRole : RoleEntryPoint
	{
		public override bool OnStart()
		{
			DiagnosticMonitorConfiguration diagnosticMonitorConfiguration = DiagnosticMonitor.GetDefaultInitialConfiguration();

			diagnosticMonitorConfiguration.Directories.ScheduledTransferPeriod = System.TimeSpan.FromSeconds(15);
			diagnosticMonitorConfiguration.Logs.ScheduledTransferPeriod = System.TimeSpan.FromSeconds(15);

			DiagnosticMonitor.Start("DiagnosticsConnectionString", diagnosticMonitorConfiguration);

			System.Diagnostics.Trace.TraceInformation("Onstart completed");

			// For information on handling configuration changes
			// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
			RoleEnvironment.Changing += RoleEnvironmentChanging;

			return base.OnStart();
		}

		private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
		{
			// If a configuration setting is changing
			if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
			{
				// Set e.Cancel to true to restart this role instance
				e.Cancel = true;
			}
		}
	}
}
